#include "AppDelegate.h"
#include "CCLuaEngine.h"
#include "SimpleAudioEngine.h"
#include "cocos2d.h"
#include "Runtime.h"
#include "manual/LuaIce.h"

using namespace CocosDenshion;

USING_NS_CC;
using namespace std;

AppDelegate::AppDelegate()
{
}

AppDelegate::~AppDelegate()
{
	SimpleAudioEngine::end();
}

float adaptDesignSize(const CCSize& minSize, const CCSize& maxSize, int maxDpi)
{
	CCSize screenSize = CCDirector::getInstance()->getOpenGLView()->getFrameSize();
	//最小横宽比,如果再小就会在上下两边留出黑边
	float minRatio = minSize.width / maxSize.height;
	//最大横宽比,如果再大就会在左右两边留出黑边
	float maxRatio = maxSize.width / minSize.height;
	float screenRatio = screenSize.width / screenSize.height;

	if (maxDpi > 0 && Device::getDPI() > maxDpi)
	{
		float ratio = float(Device::getDPI()) / float(maxDpi);
		screenSize.width /= ratio;
		screenSize.height /= ratio;
	}

	float scale = 1;
	//根据长宽比设定缩放比例
	if (screenRatio < minRatio) {
		scale = screenSize.width / minSize.width;
	} 
	else if (screenRatio > maxRatio) {
		scale = screenSize.height / minSize.height;
	}
	else {
		if (screenSize.width < minSize.width || screenSize.height < minSize.height) {
			float hscale = screenSize.width / minSize.width;
			float vscale = screenSize.height / minSize.height;

			scale = hscale < vscale ? hscale : vscale;
		}
		else if (screenSize.width > maxSize.width || screenSize.height > maxSize.height) {
			float hscale = screenSize.width / maxSize.width;
			float vscale = screenSize.height / maxSize.height;

			scale = hscale < vscale ? vscale : hscale;
		}
	}

	CCSize designSize;
	designSize.width = screenSize.width / scale;
	designSize.height = screenSize.height / scale;

	designSize.width = std::min(designSize.width, maxSize.width);
	designSize.width = std::max(designSize.width, minSize.width);
	designSize.height = std::min(designSize.height, maxSize.height);
	designSize.height = std::max(designSize.height, minSize.height);

	//缩放
	CCDirector::sharedDirector()->getOpenGLView()->setDesignResolutionSize(
		designSize.width, designSize.height,
		kResolutionShowAll
		);

	screenSize = CCDirector::sharedDirector()->getOpenGLView()->getFrameSize();
	float hscale = screenSize.width / designSize.width;
	float vscale = screenSize.height / designSize.height;
	float designRatio = designSize.width / designSize.height;
	if (screenRatio < designRatio)
		scale = hscale;
	else
		scale = vscale;

	return scale;
}

bool AppDelegate::applicationDidFinishLaunching()
{
	// initialize director
	auto director = Director::getInstance();
	auto glview = director->getOpenGLView();
	if(!glview) {
		int resWidth = UserDefault::getInstance()->getIntegerForKey("ResolutionX", -1);
		int resHeight = UserDefault::getInstance()->getIntegerForKey("ResolutionY", -1);

		if (resWidth == -1 || resHeight == -1) {
			resWidth = 960;
			resHeight = 640;
			UserDefault::getInstance()->setIntegerForKey("ResolutionX", resWidth);
			UserDefault::getInstance()->setIntegerForKey("ResolutionY", resHeight);
			UserDefault::getInstance()->flush();
		}
		glview = GLView::createWithRect("XSanguo", Rect(0,0,resWidth,resHeight));
		director->setOpenGLView(glview);
	}

	adaptDesignSize(CCSize(960, 640), CCSize(1136, 640), 300);

	// turn on display FPS
	director->setDisplayStats(true);

	// set FPS. the default value is 1.0/60 if you don't call this
	director->setAnimationInterval(1.0 / 60);

	std::vector<std::string> searchPaths;
	searchPaths.push_back("src/framework");
	FileUtils::getInstance()->setSearchPaths(searchPaths);

	auto engine = LuaEngine::getInstance();
	ScriptEngineManager::getInstance()->setScriptEngine(engine);

	engine->executeString("require \"Cocos2d\"");
	engine->executeString("require \"Cocos2dConstants\"");

	searchPaths.clear();
	FileUtils::getInstance()->setSearchPaths(searchPaths);

	luaopen_Ice (engine->getLuaStack()->getLuaState());

#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
	if (UserDefault::getInstance()->getBoolForKey("CocosIde", false))
	{
		if (startRuntime())
			return true;
	}
#endif

	engine->executeScriptFile("src/main.lua");

	return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
	Director::getInstance()->stopAnimation();

	SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
	Director::getInstance()->startAnimation();

	SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
